Wiimote1.led1 = true
Wiimote2.led1 = true
Wiimote2.led4 = true
Wiimote3.led2 = true
Wiimote4.led2 = true
Wiimote4.led4 = true
Wiimote5.led3 = true
Wiimote6.led3 = true
Wiimote6.led4 = true
//Mouse Control Script using IR
//by vkapadia with much assistance from inio
//vkapadia@vkapadia.com
//
//Calibration:
//To calibrate, run this program and put the Wiimote on a flat surface face-up.
//Then read the values in the debug line (next to the run button).
//Change these values until the debug line reads approx. all zeros.
var.xtrim = -7
var.ytrim = -17
var.ztrim = -7
//
//Options:
var.deadzone = 5 //distance in pixels that you have to move the wiimote in
   //order for it to register movement. Creates a "dead zone" around the pointer
   //to make it easier to click. Higher = smoother but less accurate.
//fake cursor init
cursor2.visible = true
//more options to be added later

//Controls:
//Point Wiimote = Move Mouse
//D-Pad = Arrow Keys
//B-Button = Left Click
//Home = Middle Click
//A-Button = Right Click
//Plus and Minus = Control Volume
//One = Unmapped
//Two = Unmapped
//
//If the pointer hits the edge of the screen, the Wiimote will rumble a bit.
//
//The LEDs attempt to emulate KITT's grill from Knight Rider

//***Do not edit anything below this line unless you know what you are doing.***
var.accx = wiimote.RawForceX + var.xtrim
var.accy = wiimote.RawForceY + var.ytrim
var.accz = wiimote.RawForceZ + var.ztrim

if wiimote.dot1vis and wiimote.dot2vis then

  if var.accy > -7 then
    var.orientation = 0
  elseif var.accy > -45 then
    if var.accx < 0 then
      var.orientation = 3
    else
      var.orientation = 1
    endif
  else
    var.orientation = 2
  endif

  if var.leftpoint = 0 then
    if var.orientation = 0 then
      if wiimote.dot1x < wiimote.dot2x then
        var.leftpoint = 1
      else
        var.leftpoint = 2
      endif
    endif
    if var.orientation = 1 then
      if wiimote.dot1y > wiimote.dot2y then
        var.leftpoint = 1
      else
        var.leftpoint = 2
      endif
    endif
    if var.orientation = 2 then
      if wiimote.dot1x > wiimote.dot2x then
        var.leftpoint = 1
      else
        var.leftpoint = 2
      endif
    endif
    if var.orientation = 3 then
      if wiimote.dot1y < wiimote.dot2y then
        var.leftpoint = 1
      else
        var.leftpoint = 2
      endif
    endif
  endif

  if var.leftpoint = 1 then
    var.fix1x = wiimote.dot1x
    var.fix1y = wiimote.dot1y
    var.fix2x = wiimote.dot2x
    var.fix2y = wiimote.dot2y
  else
    var.fix1x = wiimote.dot2x
    var.fix1y = wiimote.dot2y
    var.fix2x = wiimote.dot1x
    var.fix2y = wiimote.dot1y
  endif

  var.dx = var.fix2x - var.fix1x
  var.dy = var.fix2y - var.fix1y
  var.cx = (var.fix1x+var.fix2x)/1024.0 - 1
  var.cy = (var.fix1y+var.fix2y)/1024.0 - .75

  var.d = sqrt(var.dx*var.dx+var.dy*var.dy)

  var.dx = var.dx / var.d
  var.dy = var.dy / var.d

  var.ox = -var.dy*var.cy-var.dx*var.cx;
  var.oy = -var.dx*var.cy+var.dy*var.cx;

  var.ax = (var.ox * screen.desktopwidth) + (screen.desktopwidth / 2)
  var.ay = (-var.oy * screen.desktopwidth) + (screen.desktopheight / 2)

  var.dx = var.ax - cursor2.posx
  var.dy = var.ay - cursor2.posy

  var.d = sqrt((var.dx*var.dx)+(var.dy*var.dy))

  var.a = 180 / (200 + var.d * var.d * var.d * .001)

  if var.d <= var.deadzone then var.a = 1

  //debug = var.d + " " + var.a

  var.finalx = cursor2.posx * var.a + var.ax * (1 - var.a)
  var.finaly = cursor2.posy * var.a + var.ay * (1 - var.a)


  cursor2.posx = smooth(var.finalx,3,5)
  cursor2.posy = smooth(var.finaly,3,5)

else

  var.leftpoint = 0

endif

var.xpos = var.finalx
var.ypos = var.finaly
ppjoy1.analog0 = ensuremaprange(var.xpos,0,screen.desktopwidth,-1,1)
ppjoy1.analog1 = ensuremaprange(var.ypos,0,screen.desktopheight,-1,1)

if wiimote1.B == true
  ppjoy1.digital0 = true
else
  ppjoy1.digital0 = false
endif

debug = var.xpos+ "  " + var.ypos